package com.example.classcommunity3.controller;

import com.alibaba.fastjson.JSON;
import com.example.classcommunity3.model.Subject;
import com.example.classcommunity3.service.ClasstableService;
import com.example.classcommunity3.service.HomeworkService;
import com.example.classcommunity3.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping(value = "/subject")
public class SubjectController {

    @Autowired
    SubjectService subjectService;

    @Autowired
    HomeworkService homeworkService;

    @Autowired
    ClasstableService classtableService;

    @RequestMapping(params = "method=addSub")
    public HashMap<String, Object> addSubject(String subName, Long account) {
        HashMap<String, Object> hashMap = subjectService.addSubject(subName, account);

        return hashMap;
    }

    @RequestMapping(params = "method=delSub")
    public Boolean deleteSub(String subCode){
        try{
            subjectService.deleteSub(subCode);
            homeworkService.deleteHomeworkBySubCode(subCode);
            classtableService.deleteAllSub(subCode);
            return true;
        }catch (Exception e){
            return false;
        }

    }

    @RequestMapping(params = "method=getAllTeacherSub")
    public String getAllTacherSub(Long account) {
        /**
         @describe 得到老师开的所有课程
         @param [account]
         @return java.lang.String json->subCode;subName;subTAccount;subStuNumber;
         @date 2018/6/7
        */
        List<Subject> list = subjectService.getAllTacherSub(account);
        String jsonResult = JSON.toJSONString(list);

        return jsonResult;
    }

    @RequestMapping(params = "method=findAllHomework")
    public String findAllHomework(String subCode) {
        /**
         @describe 找到某门课程下的所有作业
         @param [subCode 课程邀请码]
         @return java.lang.String
         @date 2018/6/10
         */
        String json = homeworkService.findHomeworkBySubCode(subCode);
        System.out.println(json);
        return json;
    }

    //教师添加作业
    @RequestMapping(params = "method=addHomework")
    public Boolean addHomeWork(String subCode, String title, String item, String deadLine) {
        Long homeworkId = homeworkService.addHomeWork(subCode, title, item, deadLine);
        if (homeworkId != null) {
            return true;
        }
        return false;
    }

    //教师删除作业
    @RequestMapping(params = "method=deleteHomeworkByHid")
    public Boolean deleteHomework(Long hId){
        /**
         @describe 删除作业
         @param [hId]
         @return java.lang.Boolean
         @date 2018/6/6
        */
        return homeworkService.deleteHomeworkByHid(hId);
    }
}
